package com.github.McPlugin.util;

import com.github.McPlugin.main;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class TransmissionEffect {

    /* 玩家传送到指定的位置，传送前展示球形粒子效果 */
    public static void sphereEffect(Player player, Location destination){
        int circle = 3;
        Location location = player.getLocation();
        location.add(0, player.getHeight()/2, 0);
        for(int cir=0;cir<circle;cir++) {
            for (int i = 90; i > -90; i--) {
                double rad = Math.toRadians(i);
                double y = Math.sin(rad);
                for (int degree = 0; degree < 360; degree++) {
                    double radians = Math.toRadians(degree);
                    double x = Math.cos(rad) * Math.cos(radians);
                    double z = Math.cos(rad) * Math.sin(radians);
                    location.add(x, y, z);
                    location.getWorld().playEffect(location, Effect.HAPPY_VILLAGER, 1);
                    location.subtract(x, y, z);
                }
            }
        }
        player.teleport(destination);
    }

    /* 玩家传送到指定的，分散球星，底部加末日粒子特效 */
    public static void spreadSphereEffect(Player player, Location destination){
        Location location = player.getLocation();
        location.add(0, player.getHeight()/2, 0);
        for (int i = 90; i > -90; i-=10) {
            double rad = Math.toRadians(i);
            double y = Math.sin(rad);
            for (int degree = 0; degree < 360; degree+=20) {
                double radians = Math.toRadians(degree);
                double x = Math.cos(rad) * Math.cos(radians);
                double z = Math.cos(rad) * Math.sin(radians);
                location.add(x, y, z);
                location.getWorld().playEffect(location, Effect.HAPPY_VILLAGER, 1);
                location.subtract(x, y, z);
            }
            bottomEndEffect(player, 5);
        }
        new BukkitRunnable() {
            @Override
            public void run() {
                player.teleport(destination);
            }
        }.runTaskLater(main.plugin, 50L);
    }

    /* 环绕特效，底部加末日粒子特效*/
    public static void aroundEffect(Player player, Location destination){
        new BukkitRunnable() {
            double radis =1;
            double yChange = -90;
            double xzChange = 0;
            Location location;
            @Override
            public void run() {
                location = player.getLocation();
                location.add(0,player.getHeight()/2,0);
                double yRad = Math.toRadians(yChange);
                double y = Math.sin(yRad)*radis;
                double xzRad = Math.toRadians(xzChange);
                double x = Math.cos(xzRad)*radis;
                double z = Math.sin(xzRad)*radis;

                showParticle(x, y, z);
                showParticle(-x, y, -z);
                showParticle(x, radis-y, z);
                showParticle(-x, radis-y, -z);

                if(yChange>0 || xzChange>360){
                    bottomEndEffect(player, 5);
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            player.teleport(location.add(0,player.getHeight()*100,0));
                        }
                    }.runTaskLater(main.plugin, 50L);
                    cancel();
                }else{
                    yChange += 0.5;
                    xzChange += 1;
                }
            }

            void showParticle(double x, double y, double z){
                location.add(x, y, z);
                location.getWorld().playEffect(location, Effect.HAPPY_VILLAGER, 1);
                location.subtract(x, y, z);
            }

        }.runTaskTimer(main.plugin,0L,1L);
    }

    /* 柱形移动，底部加末日粒子特效*/
    public static void cylindricalEffect(Player player, Location destination){
        new BukkitRunnable() {
            double hight = 0;
            Location location = player.getLocation();
            @Override
            public void run() {
                location.add(0,hight,0);
                for (int i = 0; i < 360; i+=10) {
                    double radians = Math.toRadians(i);
                    double x = Math.cos(radians);
                    double y = Math.sin(radians);
                    location.add(x, 0, y);
                    location.getWorld().playEffect(location, Effect.HAPPY_VILLAGER, 1);
                    location.subtract(x, 0, y);
                }
                location.subtract(0,hight,0);
                bottomEndEffect(player, 3);
                location.add(0,hight,0);
                if(hight>player.getHeight()){
                    cancel();
                    player.teleport(destination);
                }
                hight += 0.2;
            }
        }.runTaskTimer(main.plugin, 0L,10L);
    }

    /* 没有特效，延迟delay后传送*/
    public static void noEffect(Player player, Location destination, long delay){
        new BukkitRunnable() {
            @Override
            public void run() {
                player.teleport(destination);
            }
        }.runTaskLater(main.plugin, delay);
    }

    /* 只有末日粒子特效，延迟delay后传送,太长影响效果（50L）*/
    public static void endEffect(Player player, Location destination, Long delay){
        bottomEndEffect(player, 10);
        noEffect(player, destination, delay);
    }

    /* 底部末日粒子特效 */
    public static void bottomEndEffect(Player player, int circle){
        Location location = player.getLocation();
        for(int cir=0;cir<circle;cir++){
            location.getWorld().playEffect(location, Effect.ENDER_SIGNAL, 1);
        }
    }
}

